package com.party.web.task;

import com.party.common.constant.Constant;
import com.party.common.utils.StringUtils;
import com.party.core.model.notify.TargetTemplate;
import com.party.core.service.crowdfund.ITargetTemplateService;
import com.party.web.biz.activity.ActivityBizService;
import com.party.web.biz.crowdfund.TargetBizService;
import com.party.web.biz.member.MemberIntegralNotifyService;
import com.party.web.biz.member.MemberMgmtNotifyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 定时发送短信的业务
 * @Author: yangshoukun
 * @Date: 2018/4/24 11:36
 */
@Component(value = "quartzBizSendShortMessage")
public class QuartzBizSendShortMessage {

    private static Logger logger = LoggerFactory.getLogger(QuartzBizSendShortMessage.class);

    @Autowired
    private ITargetTemplateService targetTemplateService;

    @Autowired
    private TargetBizService targetBizService;

    @Autowired
    private ActivityBizService activityBizService;

    @Autowired
    private MemberMgmtNotifyService memberMgmtNotifyService;

    @Autowired
    private MemberIntegralNotifyService memberIntegralNotifyService;

    /**
     * 需要的参数除了id, 都必须在业务的地方传给定时任务的params里面
     *
     * @param id: 业务id, 不用传到params里面!!!!!  id必须在方法的最后面!!!
     */
    public void send(Integer targetType, String id) {

        try {
            if (StringUtils.isEmpty(id)) {
                logger.error("发送短信业务的定时任务id为空");
                return;
            }

            TargetTemplate targetTemplate = targetTemplateService.get(id);
            if (null == targetTemplate) {
                logger.error("发送短信业务的定时任务id {}找不到对应的模板实体", id);
                return;
            }

            if (Constant.SEND_SHORT_MESSAGE_TYPE_CROWDFUN_PROJECT.equals(targetType)) {
                targetBizService.crowdfundRealSend(targetTemplate, targetTemplate.getInitiatorId());
            } else if (Constant.SEND_SHORT_MESSAGE_TYPE_Event.equals(targetType)) {
                targetBizService.eventRealSend(targetTemplate, targetTemplate.getInitiatorId());
            } else if (Constant.SEND_SHORT_MESSAGE_TYPE_ACTIVITY.equals(targetType)) {
                activityBizService.activityRealSend(targetTemplate, targetTemplate.getInitiatorId());
            } else if (Constant.SEND_SHORT_MESSAGE_TYPE_MEMBER.equals(targetType)) {
                memberMgmtNotifyService.kcRealSend(targetTemplate, targetTemplate.getInitiatorId());
            } else if (Constant.SEND_SHORT_MESSAGE_TYPE_INTEGRAL.equals(targetType)) {
                memberIntegralNotifyService.memberIntegralRealSend(targetTemplate,targetTemplate.getInitiatorId());
            }
        } catch (Exception e) {
            logger.error("发送短信业务的定时任务异常", e);
        }
    }
}
